#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
#Testowano na wersji PyQt4.3.3 i python 2.5.2 i python 2.4.4
import sys
from rs232_gui import Ui_Dialog
from PyQt4 import QtCore, QtGui
from options import *
from PyQt4.QtCore import pyqtSignature, QString, Qt, QVariant, SIGNAL, SLOT
from PyQt4.QtGui import *

class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
	self.port=comPort()
	self.initUi2()
	QtCore.QObject.connect(self.ui.xonRadioButton,SIGNAL("pressed()"),self.port.xon_xoff_control)
	QtCore.QObject.connect(self.ui.rtsRadioButton,SIGNAL("pressed()"),self.port.cts_rts_control)
	QtCore.QObject.connect(self.ui.dtrRadioButton,SIGNAL("pressed()"),self.port.dtr_dsr_control)
	QtCore.QObject.connect(self.ui.endLineEdit,SIGNAL("textChanged(QString)"),self.port.change_terminator)

	QtCore.QObject.connect(self.ui.portComboBox,SIGNAL("activated(QString)"),self.port.set_port)
	QtCore.QObject.connect(self.ui.speedComboBox,SIGNAL("activated(QString)"),self.port.set_speed)
	QtCore.QObject.connect(self.ui.parityComboBox,SIGNAL("activated(QString)"),self.port.set_parity)
	QtCore.QObject.connect(self.ui.fieldLenghtSpinBox,SIGNAL("valueChanged(int)"),self.port.set_databits)
	QtCore.QObject.connect(self.ui.bitsNumSpinBox,SIGNAL("valueChanged(int)"),self.port.set_stopbits)
	QtCore.QObject.connect(self.ui.polecenie,SIGNAL("returnPressed()"),self.slotPolecenie)
	QtCore.QObject.connect(self.ui.pushButton,SIGNAL("toggled(bool)"),self.port.ping)




    def slotPolecenie(self):
	#wyswietl na ekran to co będzie wysłane
	self.ui.ekran.append(self.ui.polecenie.text())	
	self.port.send_sentence(self.ui.polecenie.text())
	self.ui.polecenie.clear()
	pass
    def slotGetSentence(self, arg):
	self.ui.ekran.append(QString(arg))
    def initUi2(self):
    	#ustawienie comboboxa z dostępnymi portami
    	for portstr in self.port.devices:
		self.ui.portComboBox.addItem(QString(portstr))

	#ustawienie comboboxa z dostepnymi prędkościami
	for portspeeds in self.port.speedlist:
		self.ui.speedComboBox.addItem(QString(portspeeds))
	
	#ustawienie comboboxa z dostępnymi parzystościami
	for i in self.port.paritylist:
		self.ui.parityComboBox.addItem(QString(i))
	
	#ustawienie listboxa na domyślną wartość
	self.ui.endLineEdit.setText(QString(self.port.terminator))
	#textBoxy są puste

	#ustawienie numerów id przycisków radio
	self.ui.rtsRadioButton.setChecked(1)		
	#ustawienie parametrów portu zgodnie z tym co jest wyświetlone na formatce
	self.port.gui=self
	self.port.set_port(self.port.devices[0])

	self.port.get_attr()
	self.port.set_speed(self.ui.speedComboBox.currentText())
	self.port.set_databits(self.ui.fieldLenghtSpinBox.value())
	self.port.set_stopbits(self.ui.bitsNumSpinBox.value())
	self.port.parity=str(self.ui.parityComboBox.currentText())
	self.port.set_attr()



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())


